package.path=package.path..";../itemdb/?.lua"
require("xml")
require("lsqlite3")
require("curl")

db = sqlite3.open("armory.db")

local function gettalents(char)
	local name, realm = char:get_attribute("name"), char:get_attribute("realm")

	for data in db:urows("SELECT content FROM cache WHERE url LIKE '%character-talents.xml?r="..curl.escape(realm).."&n="..curl.escape(name).."'") do
		local talents = xml.parse(data)
		talents = talents:get_first_child("characterInfo")
		talents = talents:get_first_child("talentTab")
		talents = talents:get_first_child("talentTree")
		return talents:get_attribute("value")
	end
end

local function get_talent_value(talents, index)
	return talents and tonumber(talents:sub(index, index)) or 0
end

local modifiers = {
	Warrior = {
		stamina = { { 65, 1 } },
		strength = { { 65, 2 } },
	},
	Druid = {
		intellect = { { 35, 4 } },
		spirit = { { 57, 5 } },
	},
	Hunter = {
		agility = { { 33, 1 },  { 57, 3 } },
		intellect = { { 33, 3 } },
	},
}

local function handle_entry(data, output)
	local talents
	local info = data:get_first_child("characterInfo")
	local char = info:get_first_child("character")
	local tab = info:get_first_child("characterTab")
	if not tab or not char then return end

	local class = char:get_attribute("class")
	local race = char:get_attribute("race")
	local level = tonumber(char:get_attribute("level"))


	local rdata = output[race]
	if not rdata then
		rdata = {}
		output[race] = rdata
	end
	local cdata = rdata[class]
	if not cdata then
		cdata = {}
		rdata[class] = cdata
	end
	local ldata = cdata[level]
	if not ldata then
		ldata = {}
		cdata[level] = ldata
	end

	local stats = tab:get_first_child("baseStats")
	for stat in stats:iterate_children() do
		local name = stat.label
		if name ~= "armor" then
			local value = tonumber(stat:get_attribute("base"))
			local mods = modifiers[class] and modifiers[class][name]
			if mods then
				if not talents then
					local status, result = pcall(gettalents, char)
					if not status then
						print("UNABLE TO GET TALENTS", result, char:get_attribute("name"), char:get_attribute("realm"))
					else
						talents = result
					end
				end
				local modval = 0
				for _, mod in ipairs(mods) do
					modval = modval + get_talent_value(talents, mod[1]) * mod[2]
				end
				value = value / (1 + modval / 100)
			end
			if ldata[name] and math.abs(ldata[name] - value) > 1 then
				print("MISMATCH", race, class, level, name, value, ldata[name])
			end
			ldata[name] = math.min(value, ldata[name] or 99999)
		end
	end
	ldata.count = (ldata.count or 0) + 1
end

local output = {}
for data in db:urows("SELECT content FROM cache WHERE url LIKE '%character-sheet%'") do
	handle_entry(xml.parse(data), output)
end

print("info = {")
for race, cdata in pairs(output) do
	print(("\t[%q] = {"):format(race))
	for class, ldata in pairs(cdata) do
		print(("\t\t[%q] = {"):format(class:upper()))
		for level = 1, 70 do
			local info = ldata[level]
			if info then
				local tmp = {}
				for stat, value in pairs(info) do
					tmp[#tmp + 1] = ("%s = %s"):format(stat, value == "*" and '"*"' or value)
				end
				print(("\t\t\t[%d] = { %s },"):format(level, table.concat(tmp, ", ")))
			end
		end
		print("\t\t},")
	end
	print("\t},")
end
print("}")
